package de.mediaScan;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.mediaScan.exceptions.NoFolderFoundException;
import de.mediaScan.filesystem.FolderScanner;
import de.mediaScan.scanner.InformationReceiver;

public class MediaScanner {
	public List<Map<String, String>> getMediaInformation(final String path) {
		final List<Map<String, String>> result = new ArrayList<Map<String, String>>();

		final List<String> fileTypes = new ArrayList<String>();
		fileTypes.add("mp3");

		Logger.getLogger("org.jaudiotagger").setLevel(Level.OFF);
		final ExecutorService executor = Executors.newFixedThreadPool(10);
		final FolderScanner scanner = new FolderScanner(fileTypes);

		try {
			final List<Future<Map<String, String>>> collectedInformation = new ArrayList<Future<Map<String, String>>>();

			for (final File file : scanner.scanFolder(path)) {
				collectedInformation.add(executor.submit(new InformationReceiver(file)));
			}

			for (final Future<Map<String, String>> informationThread : collectedInformation) {
				try {
					final Map<String, String> value = informationThread.get();

					for (final String key : value.keySet()) {
						System.out.print(" - " + key + ": " + value.get(key) + " - ");
					}

					System.out.println("");
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
			}
		} catch (final NoFolderFoundException e) {
			e.printStackTrace();
		}

		executor.shutdown();

		return result;
	}
}
